package com.example.malluser.service.impl;

import com.example.mallorder.api.domain.Order;
import com.example.malluser.entity.MallUserEntity;
import com.example.malluser.mapper.MallUserMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/** @author yangtao */
@Service
@RocketMQMessageListener(consumerGroup = "mall-order", topic = "order-topic")
public class OrderSmsService implements RocketMQListener<Order> {
  @Autowired
  private MallUserMapper userDao;
  
  @Override
  public void onMessage(Order order) {
    // 下单后，发送短信给用户
    //System.out.println("下单成功，发送短信给用户" + JSON.toJSONString(order));
    //根据uid 获取手机号
    MallUserEntity user = userDao.selectById(order.getUid());

    //生成验证码 1-9 6
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 6; i++) {
      builder.append(new Random().nextInt(9) + 1);
    }
    String smsCode = builder.toString();
    Param param = new Param(smsCode);
    try {
      //SmsUtil.sendSms(user.getTelephone(), "测试短信", "SMS_764936716", JSON.toJSONString(param));
      System.out.println("短信发送成功");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  class Param {
    private String code;

    public String getCode() {
      return code;
    }

    public void setCode(String code) {
      this.code = code;
    }

    public Param(String code) {
      this.code = code;
    }
  }
}
